//
//  MYGMedicalToolsTIMIUAView.swift
//  MYDoctor
//
//  Created by 林矗 on 2024/5/27.
//

import UIKit
import JXSegmentedView

class MYGMedicalToolsTIMIUAView: UIView {
    // MARK: - Property
    private var age: Bool?
    private var risk: Bool?
    private var known: Bool?
    private var aspl: Bool?
    private var recent: Bool?
    private var marker: Bool?
    private var st: Bool?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        backgroundColor = .white
        addSubviews([
            scrollView,
            commitButton
        ])
        
        scrollView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(commitButton.snp.top)
        }
        
        commitButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(49)
            make.bottom.equalToSuperview().offset(-bottomSafeheight)
        }
        
        scrollView.addSubview(scrollContentView)
        scrollContentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
        
        let line1 = createSeparator()
        let line2 = createSeparator()
        let line3 = createSeparator()
        let line4 = createSeparator()
        let line5 = createSeparator()
        let line6 = createSeparator()
        let line7 = createSeparator()
        
        scrollContentView.addSubviews([
            chooseView1,
            line1,
            chooseView2,
            line2,
            chooseView3,
            line3,
            chooseView4,
            line4,
            chooseView5,
            line5,
            chooseView6,
            line6,
            chooseView7,
            line7,
            scoreView
        ])
        
        chooseView1.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview().inset(26)
        }
        
        line1.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView1.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView2.snp.makeConstraints { make in
            make.top.equalTo(chooseView1.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line2.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView2.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView3.snp.makeConstraints { make in
            make.top.equalTo(chooseView2.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line3.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView3.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView4.snp.makeConstraints { make in
            make.top.equalTo(chooseView3.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line4.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView4.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView5.snp.makeConstraints { make in
            make.top.equalTo(chooseView4.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line5.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView5.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView6.snp.makeConstraints { make in
            make.top.equalTo(chooseView5.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line6.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView6.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        chooseView7.snp.makeConstraints { make in
            make.top.equalTo(chooseView6.snp.bottom)
            make.left.right.equalToSuperview().inset(26)
        }
        
        line7.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalTo(chooseView7.snp.bottom)
            make.height.equalTo(0.5)
        }
        
        scoreView.snp.makeConstraints { make in
            make.top.equalTo(chooseView7.snp.bottom)
            make.height.equalTo(108)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
    }
    
    func createSeparator() -> UIView {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238)
        return view
    }
    
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()

    private lazy var scrollContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    private lazy var chooseView1: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "年龄（≥65岁）"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.age = race == 0
        }
        return view
    }()
    
    private lazy var chooseView2: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "≥3个冠心危险因子（家族史、高血压、高胆固醇、糖尿病、吸烟等）"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.risk = race == 0
        }
        return view
    }()
    
    private lazy var chooseView3: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "已知冠心病（狭窄≥50%）"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.known = race == 0
        }
        return view
    }()
    
    private lazy var chooseView4: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "1周内使用阿司匹林"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.aspl = race == 0
        }
        return view
    }()
    
    private lazy var chooseView5: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "近期（≤24h）严重心绞痛（2次以上AP或静息AP）"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.recent = race == 0
        }
        return view
    }()
    
    private lazy var chooseView6: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "心肌损伤指标升高"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.marker = race == 0
        }
        return view
    }()
    
    private lazy var chooseView7: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "ST段偏移≥0.05mm"
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {[weak self] (race) in
            self?.st = race == 0
        }
        return view
    }()
    
    
    private lazy var scoreView: MYGMedicalToolsScoreView = {
        let view = MYGMedicalToolsScoreView()
        view.level = "正常"
        view.title = "0"
        return view
    }()
    
    private lazy var commitButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .rgba(151, 179, 255)
        button.setTitle("开始计算", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .medium(17)
        button.addTarget(self, action: #selector(commit), for: .touchUpInside)
        return button
    }()
}


extension MYGMedicalToolsTIMIUAView: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        self
    }
}

extension MYGMedicalToolsTIMIUAView {
    @objc func commit() {
        guard let age = age, let risk = risk, let known = known, let aspl = aspl, let recent = recent, let marker = marker, let st = st else {
            MMYGHUD.showToast(text: "请先选择所有项")
            return
        }
        
        let score = age.int + risk.int + known.int + aspl.int + recent.int + marker.int + st.int
        if score <= 2 {
            scoreView.level = "低危"
        } else if score <= 4 {
            scoreView.level = "中危"
        } else {
            scoreView.level = "高危"
        }
        scoreView.progress = Double(score) / 7.0
        scoreView.title = "\(score)"
    }
}
